﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CompanyManagement.Models
{
    public class CompanyDAO
    {
        CompanyEntities db;

        public CompanyDAO()
        {
            db = new CompanyEntities();
        }

        public int Insert(Company company)
        {
            try
            {
                db.Companies.Add(company);
                db.SaveChanges();
                return company.ID;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool Delete(Company company)
        {
            try
            {
                var existed = db.Companies.FirstOrDefault(p => p.ID == company.ID);
                if (existed != null)
                {
                    existed.IsActive = false;
                    db.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Company> GetCompanies()
        {
            return db.Companies.Where(p=>p.IsActive == true).ToList();
        }

        public bool CheckCompanyExisted(Company existed)
        {
            var company = db.Companies.FirstOrDefault(p => p.ID != existed.ID && ((p.CompanyNo.Equals(existed.CompanyNo) && !string.IsNullOrEmpty(p.CompanyNo)) || 
                p.Name.ToLower().Equals(existed.Name.ToLower())));
            if (company != null)
                return true;
            return false;
        }

        internal Company GetCompanyByCompanyNo(string companyNo)
        {
            return db.Companies.FirstOrDefault(p => p.CompanyNo.Equals(companyNo));
        }

        internal bool Update(Company updateObj)
        {
            try
            {
                var existed = db.Companies.FirstOrDefault(p => p.ID == updateObj.ID);
                if (existed != null)
                {
                    existed.Name = updateObj.Name;
                    existed.Owner = updateObj.Owner;
                    existed.Phone = updateObj.Phone;
                    existed.ReasionDissolved = updateObj.ReasionDissolved;
                    existed.Status = updateObj.Status;
                    existed.TaxCode = updateObj.TaxCode;
                    existed.TradingName = updateObj.TradingName;
                    existed.WardCode = updateObj.WardCode;
                    existed.MSN1 = updateObj.MSN1;
                    existed.MSN2 = updateObj.MSN2;
                    existed.MSN3 = updateObj.MSN3;
                    existed.MSN4 = updateObj.MSN4;
                    existed.DistrictCode = updateObj.DistrictCode;
                    existed.DissolvedDate = updateObj.DissolvedDate;
                    existed.DirectorName = updateObj.DirectorName;
                    existed.CompanyTypeCode = updateObj.CompanyTypeCode;
                    existed.CompanyNo = updateObj.CompanyNo;
                    existed.CompanyCreateDated = updateObj.CompanyCreateDated;
                    existed.Capital = updateObj.Capital;
                    existed.BusinessReg = updateObj.BusinessReg;
                    existed.Address = updateObj.Address;
                    existed.Abbreviation = updateObj.Abbreviation;

                    db.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        internal Company GetCompanyByID(int companyID)
        {
            return db.Companies.FirstOrDefault(p=>p.ID == companyID);
        }
    }
}